package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    // 条件分页查询
    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("条件查询学生信息:{}", studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    // 路径传需要删除的id的数组
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除id为{}的学院", ids);
        studentService.delete(ids);
        return Result.success();
    }

    // post方法添加学员
    @Log
    @PostMapping
    public Result saveStudent(@RequestBody Student student) {
        log.info("添加学院:{}", student);
        studentService.saveStudent(student);
        return Result.success();
    }

    // 根据id查询学员信息
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("根据id:{}查询学员", id);
        Student student = studentService.findById(id);
        return Result.success(student);
    }

    // 修改学员信息
    @Log
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("更新学生信息:{}", student);
        studentService.updateStudent(student);
        return Result.success();
    }

    // 添加学生的惩罚
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result punishment(
            @PathVariable Integer id,
            @PathVariable Integer score
    ) {
        log.info("扣除id为{}的学生{}分", id, score);
        studentService.punishment(id, score);
        return Result.success();
    }
}
